var wxCharts = require('../../utils/wxcharts.js');
const app = getApp();

const date = new Date();
const years = [];
const months = [];
const days = [];
for (let i = 2018; i <= date.getFullYear(); i++) {
  years.push(i);
}
for (let i = 1; i <= 12; i++) {
  months.push(i);
}
for (let i = 1; i <= 31; i++) {
  days.push(i);
}

Page({
  /**
   * 页面的初始数据
   */
  data: {
    wrapDate: false,
    isshow: false,
    isshow1: false,
    dataStatus: false,
    years: years,
    year: date.getFullYear(),
    months: months,
    month: date.getMonth() + 1,
    days: days,
    day: 2,
    value: [9999, date.getMonth(), 1],
    date: date.getFullYear() + ' 年' + (date.getMonth() + 1) + '月',
    ifCanvas: false,
    totalIncome: '',
    totalOutcome: '',
    totalProfit: ''
  },

  //获取数据
  apidata: function() {
    var that = this;
    wx.showLoading({
      title: '拼命加载中',
      mask: true
    });
    wx.request({
      method: 'POST',
      url: app.preUrl + 'getOperate', //查询运营报表
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: app.globalData.userId,
        year: that.data.year,
        month: that.data.month
      },
      success: function(res) {
        wx.hideLoading();
        if (res.data.code == 100) {
          var resultdata = res.data.result;
          console.log(resultdata);
          var arr = [
            parseInt(resultdata.saleIncome), //销货收入
            parseInt(resultdata.groceriesIncome), //杂货收入
            parseInt(resultdata.rentIncome), //租金收入
            parseInt(resultdata.otherIncome), //其他收入

            parseInt(resultdata.purchaseOutcome),
            parseInt(resultdata.payOutcome),
            parseInt(resultdata.rentOutcome),
            parseInt(resultdata.repairOutcome),
            parseInt(resultdata.otherOutcome),
            parseInt(resultdata.trafficOutcome)
          ];
          if (arr[0] + arr[1] + arr[2] + arr[3] == 0) {
            that.setData({
              isshow: true
            });
          } else {
            new wxCharts({
              animation: true,
              canvasId: 'ringCanvasOne',
              type: 'pie',
              series: [
                {
                  name: '销货收入',
                  data: arr[0],
                  format: function(val) {
                    return arr[0] + '元/' + val * 100 + '%';
                  }
                },
                {
                  name: '杂货收入',
                  data: arr[1],
                  format: function(val) {
                    return arr[1] + '元/' + val * 100 + '%';
                  }
                },
                {
                  name: '租金收入',
                  data: arr[2],
                  format: function(val) {
                    return arr[2] + '元/' + val * 100 + '%';
                  }
                },
                {
                  name: '其他收入',
                  data: arr[3],
                  format: function(val) {
                    return arr[3] + '元/' + val * 100 + '%';
                  }
                }
              ],
              width: 300,
              height: 216,
              dataLabel: true
            });
            that.setData({
              isshow: false
            });
          }
          if (arr[4] + arr[5] + arr[6] + arr[7] + arr[8] == 0) {
            that.setData({
              isshow1: true
            });
          } else {
            new wxCharts({
              animation: true,
              canvasId: 'ringCanvasTwo',
              type: 'pie',
              series: [
                {
                  name: '购货支出',
                  data: arr[4],
                  format: function(val) {
                    return arr[4] + '元/' + val * 100 + '%';
                  }
                },
                {
                  name: '工资支出',
                  data: arr[5],
                  format: function(val) {
                    return arr[5] + '元/' + val * 100 + '%';
                  }
                },
                {
                  name: '租金支出',
                  data: arr[6],
                  format: function(val) {
                    return arr[6] + '元/' + val * 100 + '%';
                  }
                },
                {
                  name: '设备及维修支出',
                  data: arr[7],
                  format: function(val) {
                    return arr[7] + '元/' + val * 100 + '%';
                  }
                },
                {
                  name: '运输支出',
                  data: arr[9],
                  format: function(val) {
                    return arr[9] + '元/' + val * 100 + '%';
                  }
                },
                {
                  name: '其他支出',
                  data: arr[8],
                  format: function(val) {
                    return arr[8] + '元/' + val * 100 + '%';
                  }
                }
              ],
              width: 300,
              height: 216,
              dataLabel: true
            });
            that.setData({
              isshow1: false
            });
          }
          that.setData({
            totalIncome: res.data.result.totalIncome,
            totalOutcome: res.data.result.totalOutcome,
            totalProfit: res.data.result.totalProfit
          });
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.message,
            confirmColor: '#56BDE0',
            showCancel: false
          });
        }
      },
      fail: function(error) {
        that.setData({ buthiddenNext: true });
      }
    });
  },

  //日期展示
  wrapDateSelect: function() {
    var that = this;
    var animation = wx.createAnimation({
      duration: 300,
      timingFunction: 'linear'
    });
    that.animation = animation;
    animation.translateY(-200).step();
    that.setData({
      ifscroll: true,
      animationData: animation.export(),
      wrapDate: !that.data.wrapDate,
      dataStatus: !that.data.dataStatus,
      ifCanvas: true
    });
    setTimeout(function() {
      animation.translateY(0).step();
      that.setData({
        animationData: animation.export()
      });
    }, 0);
  },

  //隐藏弹窗事件
  hideModal: function(e) {
    var that = this;
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'linear'
    });
    that.animation = animation;
    animation.translateY(-200).step();
    that.setData({
      animationData: animation.export()
    });
    setTimeout(function() {
      animation.translateY(0).step();
      that.setData({
        ifscroll: false,
        animationData: animation.export(),
        wrapDate: false,
        dataStatus: false,
        ifCanvas: false
      });
    }, 100);
  },
  //日期类别选择
  bindChange: function(e) {
    var that = this;
    var val = e.detail.value;
    that.setData({
      year: that.data.years[val[0]],
      month: that.data.months[val[1]]
    });
  },

  //日期确定事件
  sureDateBtn: function() {
    var that = this;
    that.setData({
      date: that.data.year + '年' + that.data.month + '月'
    });
    that.hideModal();
    setTimeout(this.apidata, 500);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  onLoad: function(options) {
    setTimeout(this.apidata, 500);
  }
});
